package com.tnovoselec.android.common.ui.tabbar;

import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;

import com.tnovoselec.android.common.R;

public class RadioStateDrawable extends Drawable {

	public static final int SHADE_GRAY = 0;
	public static final int SHADE_BLUE = 1;
	public static final int SHADE_MAGENTA = 2;
	public static final int SHADE_YELLOW = 3;
	public static final int SHADE_GREEN = 4;
	public static final int SHADE_RED = 5;
	public static final int SHADE_ORANGE = 6;

	private final Bitmap bitmap;
	@SuppressWarnings("unused")
	private final Bitmap highlightBitmap;

	private Shader shader = null;
	private Shader textShader = null;

	private final Context context;

	private boolean highlight = false;

	private final String label;
	private final int width;

	public RadioStateDrawable(Context context, int imageID, String label, boolean highlight, int width, int shade) {
		this(context, imageID, label, highlight, width);
		setShade(shade);
	}

	public RadioStateDrawable(Context context, int imageID, String label, boolean highlight, int width, int startGradientColor, int endGradientColor) {
		this(context, imageID, label, highlight, width);

		int[] shades = new int[] { startGradientColor, endGradientColor };
		shader = new LinearGradient(0, 0, 0, bitmap.getHeight(), shades, null, Shader.TileMode.MIRROR);
	}

	private RadioStateDrawable(Context context, int imageID, String label, boolean highlight, int width) {
		super();
		this.context = context;
		this.label = label;
		this.highlight = highlight;
		this.width = width;

		InputStream is = context.getResources().openRawResource(imageID);
		bitmap = BitmapFactory.decodeStream(is).extractAlpha();

		highlightBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.bottom_bar_highlight);
	}

	public void setShade(int shade) {
		int[] shades = new int[2];
		switch (shade) {
			case RadioStateDrawable.SHADE_GRAY: {
				shades = new int[] { Color.LTGRAY, Color.DKGRAY };
				break;
			}
			case RadioStateDrawable.SHADE_BLUE: {
				shades = new int[] { Color.CYAN, Color.BLUE };
				break;
			}
			case RadioStateDrawable.SHADE_RED: {
				shades = new int[] { Color.MAGENTA, Color.RED };
				break;
			}
			case RadioStateDrawable.SHADE_MAGENTA: {
				shades = new int[] { Color.MAGENTA, Color.rgb(292, 52, 100) };
				break;
			}
			case RadioStateDrawable.SHADE_YELLOW: {
				shades = new int[] { Color.YELLOW, Color.rgb(255, 126, 0) };
				break;
			}
			case RadioStateDrawable.SHADE_ORANGE: {
				shades = new int[] { Color.rgb(255, 126, 0), Color.rgb(255, 90, 0) };
				break;
			}
			case RadioStateDrawable.SHADE_GREEN: {
				shades = new int[] { Color.GREEN, Color.rgb(0, 79, 4) };
				break;
			}
		}

		shader = new LinearGradient(0, 0, 0, bitmap.getHeight(), shades, null, Shader.TileMode.MIRROR);

		if (highlight) {
			textShader = new LinearGradient(0, 0, 0, 10, new int[] { Color.WHITE, Color.LTGRAY }, null, Shader.TileMode.MIRROR);
		} else {
			textShader = new LinearGradient(0, 0, 0, 10, new int[] { Color.LTGRAY, Color.DKGRAY }, null, Shader.TileMode.MIRROR);
		}
	}

	@Override
	public void draw(Canvas canvas) {
		int bwidth = bitmap.getWidth();
		int bheight = bitmap.getHeight();
		int textSize = 16;
		int contentPadding = 20;

		int x = (width - bwidth) / 2;
		int y = ((int) context.getResources().getDimension(R.dimen.tab_bar_height) - textSize - bheight - contentPadding) / 2;

		canvas.drawColor(Color.TRANSPARENT);

		Paint p = new Paint();
		p.setColor(Color.WHITE);
		p.setStyle(Paint.Style.FILL);
		p.setTextSize(textSize);
		p.setTypeface(Typeface.DEFAULT_BOLD);
		p.setFakeBoldText(true);
		p.setTextAlign(Align.CENTER);
		p.setShader(textShader);
		p.setAntiAlias(true);
		canvas.drawText(label, width / 2, y + bheight + contentPadding, p);

		p.setShader(shader);
		canvas.drawBitmap(bitmap, x, y, p);
	}

	@Override
	public int getOpacity() {
		return 0;
	}

	@Override
	public void setAlpha(int alpha) {
		// DO nothing
	}

	@Override
	public void setColorFilter(ColorFilter cf) {
		// DO nothing
	}
}
